﻿using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Scrum.Net.Utilities
{
    public class ZipHelper
    {
        public static void Zip(List<string> files, string targetFile)
        {
            using (ZipOutputStream s = new ZipOutputStream(File.Create(targetFile)))
            {
                s.UseZip64 = UseZip64.Off; // 64 Bit Modus: Dateien über 4GB
                s.SetLevel(9); //Konpression
                byte[] buffer = new byte[4096];

                foreach (string file in files)
                {
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        }
                        while (sourceBytes > 0);
                    }
                }

                s.Finish();
                s.Close();
            }
        }

        public static void UnZip(string file, string targetPath)
        {
            FastZip expander = new FastZip();
            ZipFile zFile = new ZipFile(file);

            foreach (ZipEntry entry in zFile)
            {
                expander.ExtractZip(file, targetPath, entry.Name);
            }

            zFile.Close();
        }
    }
}
